import 'package:flutter/material.dart';
import 'package:netstorage/model/search_model.dart';
import 'package:netstorage/model/search_model_item.dart';
import 'package:netstorage/utils/navigator_utils.dart';

// 对应 images 中的图标文件
const types = ['channelgroup','channelgs','channelplane','channeltrain','cruise','district','food','hotel','huodong','shop','sight','ticket','travelgroup'];

class SearchItemWidget extends StatelessWidget {
  final SearchModel? searchModel;
  final SearchModelItem? searchModelItem;
  const SearchItemWidget({super.key, this.searchModelItem, this.searchModel});

  get _item => Container(
    padding: const EdgeInsets.all(10),
    // 设置边框
    decoration: const BoxDecoration(border: Border(bottom: BorderSide(width: 0.3, color: Colors.grey))),
    child: Row(
      children: [
        _iconContainer,
        Column(
          children: [
            SizedBox(width: 300, child: _title,),
            Container(width: 300, margin: const EdgeInsets.only(top: 5), child: _subTitle,),
          ],
        )
      ],
    ),
  );

  // get _title => Text(searchModelItem?.word ?? '');
  // 上面代码是高亮处理之前的代码，下面代码是高亮处理之后的代码
  get _title {
    List<TextSpan> spans = [];
    spans.addAll(_keywordTextSpans(searchModelItem?.word, searchModel?.keyword ?? ''));
    spans.add(TextSpan(
      text: ' ${searchModelItem?.districtname} ${searchModelItem?.zonename}',
      style: const TextStyle(fontSize: 16, color: Colors.grey)
    ));
    return RichText(text: TextSpan(children: spans));
  }

  get _subTitle => RichText(text: TextSpan(
    children: [
      TextSpan(text: searchModelItem?.price ?? '', style: const TextStyle(fontSize: 16,color: Colors.orange)),
      TextSpan(text: ' ${searchModelItem?.star ?? ''}', style: TextStyle(fontSize: 12,color: Colors.grey))
    ]
  ));

  get _iconContainer => Container(
    margin: const EdgeInsets.all(1),
    child: Image(
      height: 26,width: 26,
      image: AssetImage(_typeImage(searchModelItem?.type ?? '')),
    ),
  );

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        NavigatorUtils.jumpH5(url: searchModelItem?.url ?? 'https://xdfznh.club',title: '详情');
      },
      child: _item
    );
  }

  // 不同类型的 item 返回不同的 icon
  String _typeImage(String? type) {
    String typeStr = type ?? '';
    if(typeStr.length == 0) return 'travelgroup';
    typeStr = typeStr.trim();
    for(final item in types){
      if(typeStr.contains(item)) {
        typeStr = item;
        break;
      }
    }
    return 'images/type_$typeStr.png';
  }

  // 处理高亮关键字
  List<TextSpan> _keywordTextSpans(String? word, String keyword) {
    List<TextSpan> spans = [];
    if(word == null || word.isEmpty) return spans;
    String wordL = word.toLowerCase();
    String keywordL = keyword.toLowerCase();
    TextStyle normalStyle = const TextStyle(fontSize: 16,color: Colors.black87);
    TextStyle keywordStyle = const TextStyle(fontSize: 16,color: Colors.orange);

    List<String> arr = wordL.split(keyword);
    int preIndex = 0;
    for(int i = 0;i < arr.length; i++){
      if(i != 0){
        preIndex = wordL.indexOf(keywordL, preIndex);
        spans.add(TextSpan(
            text: word.substring(preIndex,preIndex + keyword.length),
            style: keywordStyle));
      }
      String val = arr[i];
      if(val.isNotEmpty){
        spans.add(TextSpan(text: val, style: normalStyle));
      }
    }
    return spans;
  }
}
